$(document)
		.ready(
				function() {
					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();

					if (pageCount == null || pageCount == ""
							|| pageCount == "undefined") {
						pageCount = 1;

					}

					$("#menunDetail").on('hide.bs.modal', function() {

						$("#menunDialog input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);

						});

						$("#menunDialog textarea").each(function() {
							$(this).prop("value", null);

						});
					});

					$("#menunDialog").delegate('#scopeList', 'change',
							function() {

								$("#scopeVal").val($(this).val());
							});

					$("#optBtn").delegate('#saveBtn', 'click', function() {
						$("#menunDetail").attr("method", "post");
						var vrId = $("#vrId").val();
						alert("vrId---------->" + vrId)
						if (vrId == null || vrId == "" || vrId == undefined) {

							$("#menunDetail").attr("action", "save.do");
						} else {

							$("#menunDetail").attr("action", "update.do");
						}
						$("#menunDetail").submit();

					});

					$('#pagination2')
							.jqPaginator(
									{
										totalCounts : totalCount,
										pageSize : pSize,
										visiblePages : pageCount,
										currentPage : 1,

										prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',

										page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {

											var scope = $("#scope").val();

											$("#pageIndex").val(num);
											var queryUrl = "queryByPage.do";
											$
													.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex : num,
															pageSize : pSize,
															scope : scope
														},
														cache : false,
														success : function(data) {
															$(
																	"#viewResources tr:not(:first)")
																	.remove();

															var results = data.pageResult.resultList;

															for (i = 0; i < results.length; i++) {
																var menun = results[i];
																var scope = "未分类";
																var rowContent = "<tr>"
																rowContent = rowContent
																		+ "<td><input name='typeId' class='checkbox' type='checkbox' value='"
																rowContent = rowContent
																		+ menun.id
																		+ "'/>";
																rowContent = rowContent
																		+ "<td>";
																rowContent = rowContent
																		+ menun.vrCode;
																rowContent = rowContent
																		+ "</td>";
																rowContent = rowContent
																		+ "<td>";
																rowContent = rowContent
																		+ menun.vrName;
																rowContent = rowContent
																		+ "</td>";
																if (menun.scope == 0) {
																	scope = "前台网站";
																} else if (menun.scope == 1) {
																	scope = "后台管理";
																}

																rowContent = rowContent
																		+ "<td>"
																		+ scope
																		+ "</td>"
																rowContent = rowContent
																		+ "<td>"
																		+ menun.url
																		+ "</td>"
																rowContent = rowContent
																		+ "<td>"
																		+ menun.note
																		+ "</td>"
																rowContent = rowContent
																		+ "<td><button name='editBtn' objId='"
																		+ menun.id
																		+ "' class='btn btn-sm-block'>修改</button></td>";
																rowContent = rowContent
																		+ "</tr>";

																$(
																		"#viewResources")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#viewResources").delegate('#selectAll', 'click',
							function() {
								var isSelectAll = $(this).prop('checked');
								$("input[name='typeId']").each(function() {
									$(this).prop("checked", isSelectAll);
								});
							});

					/**
					 * 修改按钮事件
					 */

					$("#viewResources").delegate('[name="editBtn"]', 'click',
							function() {
								var resourceId = $(this).attr("objId");

								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									
									data : {
										id : resourceId
									},
									cache : false,

									success : function(data) {

										loadMenun(data);
									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										refresh();
									}
								});

							});

					$("#tools").delegate('#deleteBtn', 'click', function() {
						delChk = $('input[name="typeId"]:checked');

						if (delChk.length > 0) {
							if (confirm("Are you sure delete the record?")) {

								/* var delUrl = "delete.do?ids="; */
								var delUrl = basePath+"/manage/viewResource/menuns/delete.do";
								var idArr = new Array();

								for (var i = 0; i < delChk.length; i++) {
									idArr.push(delChk.get(i).value);

								}

								$.ajax({
									url : delUrl,
									type : "POST",
									data : {
										ids : idArr.toString()
									},
									cache : false,

									success : function(html) {
										alert("Delete successfully");
										refresh();
									},
									error : function(html) {
										alert("Delete failly");
										refresh();
									}
								});
							} else {
								return;
							}
						} else {
							alert("Please select the record to delete");
							return;
						}
					});

					$("#tools").delegate('#addMenun', 'click', function() {

						addUrl = "add.do"
						$.ajax({
							url : addUrl,
							dataType : "json",

							cache : false,

							success : function(menun) {
								loadMenun(menun);
							},
							error : function(html) {
								alert("系统出错");
								refresh();
							}
						});

						$("#menunDialog input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);

						});

						$("#menunDialog textarea").each(function() {
							$(this).prop("value", null);

						});
						$("#menunDialog").modal('show');

					});

				});

function loadMenun(menun) {

	$("#vrId").val(menun.id);
	$("#vrCode").val(menun.vrCode);
	$("#vrName").val(menun.vrName);
	$("#url").val(menun.url);
	$("#scopeVal").val(menun.scope);
	$("#scopeList").val(menun.scope);
	$("#note").val(menun.note);
	$("#menunDialog").modal('show');
}

function vaildate(form) {
	return true;
}

function refresh() {
	window.location.reload();
}
